import { ref } from "vue";
import { defineStore } from "pinia";
import { SysSetting } from "@/api-services/models";
import settingService from "@/api-services/apis/sysSetting";
import authService from "@/api-services/apis/auth";

export const useCommonStore = defineStore("common", () => {
  const finderImages = ref<string[]>([]);
  const loginInfo = ref<any>({});
  const allSetting = ref<SysSetting[]>([]);

  function addImages(src: string) {
    finderImages.value.push(src);
    //返回封面图索引
    return finderImages.value.length - 1;
  }
  /**
   * 清空当前图片合集
   */
  function clearImages() {
    finderImages.value = [];
  }

  /**
   * 保存登录信息
   * @param _loginInfo
   */
  function setLoginInfo(_loginInfo: any) {
    loginInfo.value = _loginInfo;
  }

  /**
   * 加载全部设置
   */
  async function loadAllSetting() {
    allSetting.value = await settingService.AllSetting();
  }
  async function getLoginInfo() {
    loginInfo.value = await authService.GetLoginInfo();
  }

  function getSettingValue(key: string) {
    var settingArr = allSetting.value.filter((x) => x.key == key);
    if (settingArr.length == 0) {
      return undefined;
    }
    return settingArr[0].value;
  }
  return {
    finderImages,
    loginInfo,
    addImages,
    clearImages,
    setLoginInfo,
    allSetting,
    loadAllSetting,
    getLoginInfo,
    getSettingValue,
  };
});
